//
//  ContactDetailViewController.m
//  Contacts
//
//  Created by 李 雷川 on 14-7-16.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "ContactDetailViewController.h"
#import "ContactAddressCell.h"
#import "ContactUserInfoCell.h"
#import "ContactPhoneCell.h"
#import "ContactNotesCell.h"
#import "ContactMobileCell.h"
#import "Contact.h"
#import "Department.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/AddressBookDefines.h>
#import "ZCAddressBook.h"
#import "JGProgressHUD.h"
#import "FileInterface.h"

@interface ContactDetailViewController ()<UITableViewDataSource,UITableViewDelegate,ABNewPersonViewControllerDelegate>{
    IBOutlet  UITableView  *detailTableView;
}
@property(nonatomic, strong)IBOutlet  UITableView  *detailTableView;

@end

@implementation ContactDetailViewController
@synthesize contact,detailTableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.detailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.title = @"用户详情";
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
  
}
-(IBAction)onCallupMobilePhone:(id)sender{
   
    NSString *urlString =[NSString stringWithFormat:@"tel://%@",contact.mobilephone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    [FileInterface saveContactInfo:contact.userID];
}
-(IBAction)onCallupPhone:(id)sender{
    NSString *urlString =[NSString stringWithFormat:@"tel://%@",contact.phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    [FileInterface saveContactInfo:contact.userID];
}
-(IBAction)onPostMessage:(id)sender{
    NSString *urlString =[NSString stringWithFormat:@"sms://%@",contact.mobilephone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        [FileInterface saveContactInfo:contact.userID];
}

    //新增联系人
-(IBAction)addPeople:(id)sender{

   BOOL isSucceed=[[ZCAddressBook shareControl]addContactName:contact.name phoneNum:contact.phone
                   telephoneNum:contact.mobilephone department:contact.department.name position:contact.position mark:contact.mark withLabel:@"手机"];
    NSLog(@"添加是否成功%d",isSucceed);
    JGProgressHUD  *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleLight];
    if (isSucceed) {
        HUD.textLabel.text = @"添加成功";
        
    }
    else{
         HUD.textLabel.text = @"添加失败";
    }

     [HUD showInView:self.view animated:YES];
    [HUD dismissAfterDelay:2.0];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)initWithContact:(Contact *)_contact{
    self.contact = _contact;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:{
            ContactUserInfoCell     *cell = (ContactUserInfoCell *)[tableView dequeueReusableCellWithIdentifier:@"UserInfo"];
            cell.departmentLabel.text = contact.department.name;
            CGSize departmenSize  =   [cell.departmentLabel sizeThatFits:CGSizeMake(142, 300)];
            return departmenSize.height + 100;
        }
            break;
        case 1:
            return 60;
            break;
        case 2:
            return 60;
            break;
        case 3:
            return 84;
            break;
        case 4:
            return 100;
            break;
            
        default:
            break;
    }
    return 0;
}
#pragma UITableViewDataSource

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    return @"记者部";
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    switch (indexPath.row) {
        case 0:{
            cell = (ContactUserInfoCell *)[tableView dequeueReusableCellWithIdentifier:@"UserInfo"];
            ContactUserInfoCell  *infoCell = (ContactUserInfoCell *)cell;
            infoCell.nameLabel.text = contact.name;
            infoCell.departmentLabel.text = contact.department.name;
            //infoCell.departmentLabel.adjustsFontSizeToFitWidth= YES;
            [infoCell.departmentLabel sizeToFit];
            infoCell.officeLabel.text = contact.officeNo;
            infoCell.positionLabel.text = contact.position;
            NSString *fullImagePath = [IMAGE_FORDER stringByAppendingPathComponent:contact.headName];
            if ([[NSFileManager defaultManager]fileExistsAtPath:fullImagePath]) {
                infoCell.headImageView.image =[UIImage imageWithContentsOfFile:fullImagePath];
            }
            else{
                [ infoCell.headImageView setImageWithURL:[NSURL URLWithString:contact.headUrl] placeholderImage:
                 [UIImage imageNamed:@"logo.png"]];
            }
            
        }
            break;
        case 1:{
            cell = (ContactMobileCell *)[tableView dequeueReusableCellWithIdentifier:@"Mobile"];
            ContactMobileCell  *mobileCell = (ContactMobileCell *)cell;
            mobileCell.mobileLabel.text = contact.mobilephone;
        }
            break;
        case 2:{
            cell = (ContactPhoneCell *)[tableView dequeueReusableCellWithIdentifier:@"Phone"];
            ContactPhoneCell  *phoneCell = (ContactPhoneCell *)cell;
            phoneCell.phoneLabel.text = contact.phone;

        }
            break;
        case 3:{
            cell = (ContactNotesCell *)[tableView dequeueReusableCellWithIdentifier:@"Notes"];
              ContactNotesCell  *noteCell = (ContactNotesCell *)cell;
            noteCell.notesLabel.text = contact.mark;
        }
            break;
        case 4:{
            cell = (ContactAddressCell *)[tableView dequeueReusableCellWithIdentifier:@"Address"];
        }
            break;
            
        default:
            break;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}


- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person{
    [newPersonView dismissViewControllerAnimated:YES completion:^{
        
    }];
}
@end
